package kickboxing.view;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.Session;

import kickboxing.controller.NewsyManager;
import kickboxing.model.Newsy;
import kickboxing.model.Uzytkownik;

public class NewsyAction extends HttpServlet {
        private static final long serialVersionUID = 1L;
       
    public NewsyAction() {
        super();
    }

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        }

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                response.setContentType("text/html; charset=UTF-8");
                request.setCharacterEncoding("UTF-8");
                Newsy news  = new Newsy();
                NewsyManager linkController = new NewsyManager();
                HttpSession sesja = request.getSession(true);
                
                Uzytkownik user = (Uzytkownik) sesja.getAttribute("user");
                if(user != null){
                        try {
                        		
                                BeanUtils.populate(news, request.getParameterMap());
                                System.out.println("temat: " + news.getTemat() );
                                linkController.dodajNowyNews(news);
                                String message = "Dodano nowy news!";
                                request.setAttribute("message", message);
                                String x = "message="+message;
                                
                                
                                response.sendRedirect("admin/adminPanel.jsp?"+x);
                                
                               
                                //RequestDispatcher dispatcher = request.getRequestDispatcher("admin/adminPanel.jsp");
                                //dispatcher.forward(request, response); 
                                
                        } catch (IllegalAccessException e) {
                                e.printStackTrace();
                        } catch (InvocationTargetException e) {
                                e.printStackTrace();
                        }
                }else{
                        response.sendError(404, "Brak Uprawinien");
                }
        }

}